home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / graphicgems4.lha / GemsIV / graph_layout / fileio.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  5.2 KB  |  198 lines

  1. /****************************************************************************
  2. **    TEST FILE FOR graph (Dynamic Layout Alg)
  3. **
  4. **    MODUL   - BUFFERED FILE INPUT WITH SYNTAX CHECK
  5. **        AND NOT BUFFERED FILE OUTPUT
  6. **
  7. ** Author: dr. Szirmay-Kalos Laszlo (szirmay@fsz.bme.hu)
  8. **       Technical University of Budapest, Hungary
  9. *****************************************************************************/
  10.  
  11. #ifdef MSWINDOWS
  12. #include "fileio.hxx"
  13. #else
  14. #include "fileio.hxx"
  15. #endif
  16.  
  17. /*----------------------   Get ---------------------------------*/
  18. /* Buffered character input from the opened file        */
  19. /* IN  : character address                    */
  20. /* OUT : char is put to the address                */
  21. /*     ret = was it succesful                    */
  22. /*--------------------------------------------------------------*/
  23. BOOL FileIO :: Get( char * pc )
  24. {
  25.     if ( buffpt == nbytes ) {
  26.     if ( (nbytes = fread( buffer, 1, BUFFERSIZE, file)) <= 0) return FALSE;
  27.     else                              buffpt = 0;
  28.     }
  29.     *pc = buffer[ buffpt++ ];
  30.     return TRUE;
  31. }
  32.  
  33. /*------------------     GetString  ----------------------------*/
  34. /* Gets a string from a opened file. String ends are space, tab */
  35. /* EOL(\n) and operators( =, : ). Parts between # end EOL are    */
  36. /* ignored ( comments )                        */
  37. /* IN  : string buffer adress and maximal length        */
  38. /* OUT : was it succesful ?                    */
  39. /*     string is put to the address s                */
  40. /*--------------------------------------------------------------*/
  41. BOOL FileIO :: GetString ( pchar s, int maxlength )
  42. {
  43.     int     ichar = 0;
  44.     char last_char;
  45.     BOOL iscomment = FALSE;
  46.  
  47.     for ( ; ; ) {
  48. /*
  49. *    GET CHARACTER
  50. */
  51.     if ( !Get ( &last_char ) )  return FALSE;
  52.  
  53.     switch ( last_char ) {
  54. /*
  55. *    COMMENT
  56. */
  57.     case '#':
  58.         iscomment = TRUE;
  59.         break;
  60. /*
  61. *    OPERATORS
  62. */
  63.     case ':':
  64.     case '=':
  65.         if ( !iscomment ) {
  66.         if (ichar > 0) UnGet();            // unget operator
  67.         else           s[ichar++] = last_char;
  68.         s[ichar] = '\0';
  69.         return TRUE;
  70.         } else break;
  71. /*
  72. *    SEPARATORS
  73. */
  74.     case '\n':
  75.         iscomment = FALSE;
  76.         line_count++;
  77.     case '\t':
  78.     case ' ':
  79.         if ( !iscomment ) {
  80.         if ( ichar > 0 ) {
  81.             s[ichar] = '\0';
  82.             return TRUE;
  83.         }
  84.         }
  85.         break;
  86. /*
  87. *    KEYWORDS AND VARIABLES
  88. */
  89.     default:
  90.         if ( !iscomment ) {
  91.         if ( ichar == maxlength ) {
  92.             return FALSE;
  93.         } else {
  94.             s[ichar++] = last_char;
  95.             break;
  96.         }
  97.         } else  break;
  98.     }
  99.     }
  100. }
  101.  
  102. /*------------------     OpenFile   ----------------------------*/
  103. /* Opens a TEXT file for the defined operation            */
  104. /* IN  : file name                        */
  105. /* OUT : was it succesful ?                    */
  106. /* SIDE EFFECT: - internal file descriptor is created end line    */
  107. /*          is initialized                */
  108. /*--------------------------------------------------------------*/
  109. BOOL FileIO :: OpenFile( pchar name )
  110. {
  111.     line_count = 1;
  112.     buffpt = 0;
  113.     nbytes = 0;
  114.  
  115.     if ( (file = fopen( name, operation )) == NULL ) {
  116.     return FALSE;
  117.     }
  118.     return TRUE;
  119. }
  120.  
  121. /*------------------     CloseFile   ---------------------------*/
  122. /* Closes the opened file                    */
  123. /*--------------------------------------------------------------*/
  124. void FileIO :: CloseFile ( )
  125. {
  126.     fclose ( file );
  127. }
  128.  
  129. /*------------------     GetKeyWord  ---------------------------*/
  130. /* Gets a string and compares with the pattern keyword        */
  131. /* IN  : pattern keyword                    */
  132. /* OUT : is the specified pattern received ?            */
  133. /*--------------------------------------------------------------*/
  134. BOOL FileIO :: GetKeyWord ( pchar key )
  135. {
  136.     if ( !GetString( s, MAXSTRING ) ) return FALSE;
  137.     return GetKeyAgain( key );
  138. }
  139.  
  140. /*------------------     GetKeyAgain ---------------------------*/
  141. /* Gets the last inspected string again and compares with the    */
  142. /* pattern keyword                        */
  143. /* IN  : pattern keyword                    */
  144. /* OUT : is the specified pattern received ?            */
  145. /*--------------------------------------------------------------*/
  146. BOOL FileIO :: GetKeyAgain ( pchar key )
  147. {
  148.     if ( strcmp( key, s ) == 0 )      return TRUE;
  149.     else {
  150.     return FALSE;
  151.     }
  152. }
  153.  
  154. /*------------------     GetVariable ---------------------------*/
  155. /* Gets a double variable and compares to the specified range    */
  156. /* IN  : address of var and min, max of range            */
  157. /* OUT : was it succesful, and the received var            */
  158. /*--------------------------------------------------------------*/
  159. BOOL FileIO :: GetVariable ( double *pv, double minv, double maxv )
  160. {
  161.     if ( !GetString(s,MAXLINE) ) return FALSE;
  162.  
  163.     if ( sscanf(s,"%lf", pv ) != 1 ) {
  164.     return FALSE;
  165.     } else {
  166.     if ( *pv < minv || *pv > maxv ) {
  167.         return FALSE;
  168.     }
  169.     return TRUE;
  170.     }
  171. }
  172.  
  173. /*------------------     GetOperator ---------------------------*/
  174. /* Gets a non-white character and compares with the specified    */
  175. /* operator                            */
  176. /* IN  : specified operator                    */
  177. /* OUT : was it the specified operator                */
  178. /*--------------------------------------------------------------*/
  179. BOOL FileIO :: GetOperator ( char op )
  180. {
  181.     if ( !GetString( s, 1 ) ) return FALSE;
  182.     if ( s[0] != op ) {
  183.     return FALSE;
  184.     }
  185.     return TRUE;
  186. }
  187.  
  188. /*------------------     PutString   ---------------------------*/
  189. /* Writes the given string to the file without buffering    */
  190. /* IN  : specified string                    */
  191. /* OUT : was it succesful                    */
  192. /*--------------------------------------------------------------*/
  193. BOOL FileIO :: PutString ( pchar s )
  194. {
  195.     if ( fwrite( s, 1, strlen(s), file ) == 0) return FALSE;
  196.     else                       return TRUE;
  197. }
  198.